package com.wstuo.common.sso.service;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import net.sf.json.JSONObject;

import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.wstuo.common.sso.dao.ILoginDAO;
import com.wstuo.common.sso.dto.SSOUserDTO;
import com.wstuo.common.sso.util.ConnUtil;

public class LoginService implements ILoginService {
	@Autowired
	private ILoginDAO loginDao;

	public Boolean loginServcie(String account) {
		// TODO Auto-generated method stub
		return null;
	}

	public String getUserPassword(String userName) {
		// TODO Auto-generated method stub
		return null;
	}

	public String base64Decode(String in) {
		// TODO Auto-generated method stub
		return null;
	}

	public SSOUserDTO getBtUserInfoByLogin(String login) {
		// TODO Auto-generated method stub
		return null;
	}

	/*public Boolean loginServcie(String account) {
		int num = loginDao.login(account);
		return num > 0 ? true : false;
	}
	*//**
	 * 获取北塔用户信息
	 * @param login
	 * @return
	 *//*
	public SSOUserDTO getBtUserInfoByLogin(String login){
		SSOUserDTO dto = null;
		HttpClient httpclient = new DefaultHttpClient();
		ConnUtil cu = ConnUtil.getInstance();
		String url = "http://"+cu.getIp()+":"+cu.getPort()+"/aaa/users/"+login+"/account";
		HttpGet  httpget = new HttpGet(url); 
		InputStream is =null;
		try {
			String userJson = "";
			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httpget);
			System.out.println("StatusCode:"+response.getStatusLine().getStatusCode());
			if(response.getStatusLine().getStatusCode()==200){
				is = response.getEntity().getContent();
				BufferedInputStream bis = new BufferedInputStream(is);
				ByteArrayBuffer baf = new ByteArrayBuffer(20);
		
				int current = 0;
				while((current = bis.read()) != -1){
					baf.append((byte)current);
				}
				userJson = new String(baf.toByteArray(),"utf-8"); //指定编码
				if(userJson!=null){
					try {
						Object object = JSONUtil.deserialize(userJson);
						dto=(SSOUserDTO)JSONObject.toBean(JSONObject.fromObject(object),Class.forName("com.wstuo.common.sso.dto.BtUserDTO"));
						dto.setPassword(base64Decode(dto.getPassword()));
					} catch (JSONException e) {
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is!=null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return dto;
	}

	*//**
	 * 获取北塔的用户密码
	 * 
	 * @return
	 *//*
	public String getUserPassword(String userName){
		HttpClient httpclient = new DefaultHttpClient();
		ConnUtil cu = ConnUtil.getInstance();
		String url = "http://"+cu.getIp()+":"+cu.getPort()+"/aaa/users/"+userName+"/password";
		HttpGet  httpget = new HttpGet(url); 
		String password = "";
		InputStream is =null;
		try {
			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httpget);
			if(response.getStatusLine().getStatusCode()==200){
				is = response.getEntity().getContent();
				BufferedInputStream bis = new BufferedInputStream(is);
				ByteArrayBuffer baf = new ByteArrayBuffer(20);
		
				int current = 0;
				while((current = bis.read()) != -1){
					baf.append((byte)current);
				}
				password = new String(baf.toByteArray(),"utf-8"); //指定编码
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is!=null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return password;
		
	}
	
	
	public String base64Decode(String in) {
		Base64 base64 = new Base64();
		String out = in;
		if (in != null) {
			try {
				out = new String(base64.decode(in.getBytes()), "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		return out;
	}*/
}
